//
//  AirplaneInfoMgr.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-5-26.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//
//  舰载机信息管理类

#import "AirplaneInfoMgr.h"

@implementation AirplaneInfoMgr

@synthesize infos = _infos;

static AirplaneInfoMgr *sharedDataManager = nil;

+(AirplaneInfoMgr *) sharedManager
{
    @synchronized(self)
    {
        if (sharedDataManager == nil)
        {
            [[self alloc] init];
        }
    }
    
    return sharedDataManager;
}

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if(sharedDataManager == nil)
        {
            sharedDataManager = [super allocWithZone:zone];
            return sharedDataManager;
        }
    }
    
    return nil;
}

-(id) init
{
	if( (self=[super init])) 
    {
        self.infos = [CCArray array];
    }
    
	return self;
}

- (void) initConfig:(NSString*)XMLFileName
{
    tbx = [[TBXML tbxmlWithXMLFile:XMLFileName] retain]; 
    TBXMLElement *root = tbx.rootXMLElement;
    if(root == nil) return;
    
    //读取模型的信息
    TBXMLElement * element = [TBXML childElementNamed:@"node" parentElement:root];
    
    //循环读取关卡节点里面的敌人的信息节点，自到敌人节点读取完毕
    while (element)   
    { 
        //将关卡的节点读取成响应的info,并且保存到数组里面
        AirplaneInfo * info = [AirplaneInfo infoWithTbxml:element];
        if (info.airplaneId != 0) //非0索引存储
        {
            [_infos addObject:info];
            CCLOG(@"当前堆栈的场景信息个数为%d", [_infos count]);
        }
        
        //读取下一个敌人的兄弟节点
        element = [TBXML nextSiblingNamed:@"node" searchFromElement:element]; 
    }
}


-(AirplaneInfo *) getInfoById:(int) airplaneId
{
    AirplaneInfo * result = nil;
    
    for (AirplaneInfo * item in _infos) 
    {
        if([item airplaneId] == airplaneId)
        {
            result = item;
        }
    }
    
    return result;
}

-(void) dealloc
{
    if(tbx)
    {
        [tbx release];
        tbx = nil;
    }
    
    if(_infos)
    {
        [_infos removeAllObjects];
        [_infos release];
        _infos = nil;
    }
    
    // 不要忘记调用"super dealloc" 
    [super dealloc];
}
@end
